<template>
  <div class="order-confirm w" v-loading="pageData.loading">
    <el-card class="box-card" shadow="always">
      <el-table :data="resData.orderItemVoList" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品名称：">
                <div>{{ props.row.productName }}</div>
              </el-form-item>
              <el-form-item label="所属店铺：">
                <div>樱花树下-等花开</div>
              </el-form-item>
              <el-form-item label="小小卖家：">
                <span>{{ props.row.sellerName }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="" prop="id">
          <template slot-scope="scope">
            <img :src="scope.row.productImage" alt="" style="width:80px;height:80px;border:1px solid #fafafa">
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="productName">
        </el-table-column>
        <el-table-column label="单价(￥)" prop="currentUnitPrice">
        </el-table-column>
        <el-table-column label="数量(件)" prop="quantity">
        </el-table-column>
        <el-table-column label="小计(￥)" prop="totalPrice">
        </el-table-column>
      </el-table>

      <div class="submit-con">
        <span class="submit-tile">订单总价(￥)：</span>
        <span class="submit-total">{{resData.productTotalPrice}}</span>
        <span class="btn order-submit" @click="submitOrder">提交订单</span>
      </div>
    </el-card>
  </div>
</template>

<script>
import Tips from "@/utils/tipsUtil";
import Order from "@/service/order-service";
import RulesUtil from "@/utils/rulesUtil";
const _tips = new Tips();
const _order = new Order();
const _regular = new RulesUtil();
export default {
  props: ["addressId"],
  data() {
    return {
      //页面用到的数据
      pageData: {
        loading: true, //加载
        addressIdFromFather:null
      },

      //请求缓存的数据
      resData: {
        orderItemVoList: [],
        productTotalPrice: 0
      }
    };
  },
  /**
   * 监听从父组件传递过来的addressId属性，缓存起来
   */
  watch: {
    addressId(newVal,OldVa) {
      this.pageData.addressIdFromFather = newVal
    }
  },
  components: {},
  mounted() {
    this.init();
  },
  methods: {
    /**
     * 初始化加载订单确认的商品信息
     */
    init() {
      const vue = this;
      _order.getOrderItemByCart().then(
        res => {
          //缓存订单子明细、图片主机地址、商品总价
          vue.resData.orderItemVoList = res.data.orderItemVoList;
          const imageHost = res.data.imageHost;
          vue.resData.productTotalPrice = res.data.productTotalPrice;

          for (let i = 0; i < vue.resData.orderItemVoList.length; i++) {
            let orderItem = vue.resData.orderItemVoList[i];
            orderItem.uri = imageHost + orderItem.productImage;
          }

          //关闭加载
          vue.pageData.loading = false;
        },
        err => {
          //关闭加载
          vue.pageData.loading = false;
          _tips.notifyTips(vue, err.msg);
        }
      );
    },

    /**
     * 提交订单的方法
     */
    submitOrder(){
      const vue = this
      const shipping = vue.pageData.addressIdFromFather
      if(shipping){
        _order.createOrder(shipping).then(res=>{
          const orderNo = res.data.map(function(order){
            return order
          }).join(',');
          if(orderNo){
            vue.$router.push({path:'/pay',name:'Payment',query:{orderNo:orderNo}})
          }else{
            _tips.notifyTips(vue, '获取的订单号失败！');
          }
        },err=>{
           _tips.notifyTips(vue, err.msg);
        })
      }else{
        _tips.notifyTips(vue, '请选择收货地址');
      }
    }
  }
};
</script>

<style scoped lang="scss">
//订单确认-展开的样式
.demo-table-expand {
  font-size: 0;
  padding: 0 10px;
  background-color: #fafafa;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
  font-weight: bold;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}
//底部提交
.submit-con {
  background: #f5f7fa;
  text-align: right;
  .submit-tile {
    font-size: 15px;
  }
  .submit-total {
    margin-right: 20px;
    font-size: 18px;
    color: #409eff;
    font-weight: 700;
    vertical-align: middle;
  }
  .order-submit {
    height: 50px;
    line-height: 50px;
    width: 100px;
    text-align: center;
  }
}
</style>
